﻿using System;
using System.Linq;
using System.Windows;
using AuchanScan.ViewModels;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace AuchanScan.Views
{
    public partial class BarCodePage : PhoneApplicationPage
    {

        public BarCodePage()
        {
            InitializeComponent();
            this.Loaded += (s, e) =>
            {

                Display(!string.IsNullOrEmpty(ViewModelLocator.MainViewModelStatic.Datas.BarCode));
            };
        }

        private void Display(bool v)
        {
            if (v)
            {
                CardGrid.Visibility = Visibility.Visible;
                CreateCardGrid.Visibility = Visibility.Collapsed;
                if (appBar1 != null)
                {
                    appBar1.IsVisible = true;
                }
            }
            else
            {
                CardGrid.Visibility = Visibility.Collapsed;
                CreateCardGrid.Visibility = Visibility.Visible;
                if (appBar1 != null)
                {
                    appBar1.IsVisible = false;
                }
            }
        }

        private void Edit_Click(object sender, EventArgs e)
        {
            Display(CardGrid.Visibility == System.Windows.Visibility.Collapsed);

        }

        private void Save_Click(object sender, EventArgs e)
        {
            Display(true);
        }

        private void Pin_Click(object sender, EventArgs e)
        {
            var uri = "/Views/BarCodePage.xaml";
            if(ShellTile.ActiveTiles.FirstOrDefault((d)=>d.NavigationUri.OriginalString==uri)==null)
            ShellTile.Create(new Uri(uri,UriKind.Relative),new StandardTileData(){Title = "Auchan Fidélité", BackgroundImage = new Uri("/BackgroundFid.png",UriKind.Relative)} );
        }

       
    }
}